package com.example.demo02.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo02.common.Result;
import com.example.demo02.entity.Employee;
import com.example.demo02.exception.ServiceException;
import com.example.demo02.service.EmployeeService;
import com.example.demo02.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Employee)表控制层
 *
 * @author makejava
 * @since 2024-08-30 21:11:45
 */

@CrossOrigin
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    /**
     * 新增用户信息
     */
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee) {
        try {
            employeeService.save(employee);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        return Result.success();
    }



    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Employee currentEmployee = TokenUtils.getCurrentEmp();
        if(currentEmployee != null && currentEmployee.getId() != null && id.equals(currentEmployee.getId())){
            throw new ServiceException("不能删除当前用户信息");
        }
        employeeService.removeById(id);
        return Result.success();
    }


    /**
     * 批量删除用户信息
     */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {  //  [7, 8]
        Employee currentEmployee = TokenUtils.getCurrentEmp();
        if(currentEmployee != null && currentEmployee.getId() != null && ids.contains(currentEmployee.getId())){
            throw new ServiceException("不能删除当前用户信息");
        }
        employeeService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 查询全部用户信息
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Employee> employeeList = employeeService.list(new QueryWrapper<Employee>().orderByDesc("id"));  // select * from employee order by id desc
        return Result.success(employeeList);
    }

    /**
     * 根据ID查询用户信息
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {

        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }


    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String username,
                               @RequestParam String name) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<Employee>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(username), "username", username);
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
        // select * from employee where username like '%#{username}%' and username like '%#{username}%'
        Page<Employee> page = employeeService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

}
